﻿using Microsoft.Extensions.DependencyInjection.Extensions;
using WebApiOrm.Provider;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注册Orm提供者
        /// </summary>
        /// <typeparam name="TOrmProvider"></typeparam>
        /// <param name="services"></param>
        /// <param name="ormName"></param>
        /// <returns></returns>
        public static IServiceCollection AddOrmProvider<TOrmProvider>(this IServiceCollection services, string ormName)
            where TOrmProvider : class, IOrmProvider
        {
            services.TryAddTransient<IOrmFactory, DefaultOrmFactory>();
            services.TryAddTransient<TOrmProvider>();
            services.Configure<OrmFactoryOptions>(option => option.SetProvider<TOrmProvider>(ormName));
            return services;
        }
    }
}
